//
//  ClassifyViewController.m
//  waiMai
//
//  Created by LONG on 16/4/16.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ClassifyViewController.h"
#import "DetailViewController.h"
#import "WJDropdownMenu.h"
#import "MainTableViewCell.h"
#import "MainDetailData.h"
#import "MJChiBaoZiHeader.h"
#import "MJChiBaoZiFooter.h"
@interface ClassifyViewController ()<WJMenuDelegate,UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UITableView *myTableView;
@property(nonatomic,strong) NSMutableArray *modelArray;

@end

@implementation ClassifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"美食";
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:18],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    //nav底部线消失
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBarTintColor:UIColorFromRGB(MYColor)];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate =(id)self;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];
    
    self.view.backgroundColor = UIColorFromRGB(0xffffff);
    
    
    [self createModelsWithCount:10];
    [self myTableView];
    [self WJMenu];
}
- (UITableView *)myTableView{
    if (nil == _myTableView){
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 31, WIDTH, HEIGHT-31)];
        _myTableView.backgroundColor = [UIColor whiteColor];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        [_myTableView registerClass:[MainTableViewCell class] forCellReuseIdentifier:NSStringFromClass([MainTableViewCell class])];
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 10)];
        _myTableView.tableFooterView = view;
        
        // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
        _myTableView.mj_header = [MJChiBaoZiHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadLastData方法）
        _myTableView.mj_footer = [MJChiBaoZiFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadLastData)];
        _myTableView.mj_footer.automaticallyChangeAlpha = YES;
        // 马上进入刷新状态
        [_myTableView.mj_header beginRefreshing];

        [self.view addSubview:_myTableView];
    }
    return _myTableView;
}
-(void)loadNewData{
    [_myTableView.mj_header endRefreshing];

}
-(void)loadLastData{
    
    
    [_myTableView.mj_footer endRefreshing];

}
- (void)createModelsWithCount:(NSInteger)count
{
    if(_modelArray==nil)
    {
        _modelArray = [NSMutableArray array];
    }
    
    
    
    NSArray *iconImageNamesArray=@[@"1.5",
                                   @"2",
                                   @"3.5",
                                   @"4",
                                   @"4.5"];
    NSArray *nameArray = @[@"黄英",
                           @"莫顿",
                           @"王小姐",
                           @"SKHuang",
                           @"Kim"];
    
    NSArray *locationArray =@[@"广东省广州市白云区黄石东路3",
                              @"上海市闵行区吴宝路848号",
                              @"中国广东佛山",
                              @"天河公园",
                              @"广东省广州市白云区沙涌北涌南街79号",];
    
    NSArray *picImageNamesArray = @[ @"6",
                                     @"7",
                                     @"8",
                                     @"9",
                                     @"5",
                                     @"10",
                                     @"2",
                                     @"3",
                                     @"4"
                                     ];
    
    
    //根据传进来的数字创建对应的模型个数
    for(int i=0;i<count;i++)
    {
        int iconNameIndex = arc4random_uniform(4);
        
        
        MainDetailData *data = [MainDetailData new];
        data.image = picImageNamesArray[iconNameIndex];
        data.name=nameArray[iconNameIndex];
        data.fen=iconImageNamesArray[iconNameIndex];
        data.numer=picImageNamesArray[iconNameIndex];
        data.price=picImageNamesArray[iconNameIndex];
        data.juli = picImageNamesArray[iconNameIndex];
        data.favourable = locationArray[iconNameIndex];
        
        
        [self.modelArray  addObject:data];
        
    }
}

- (void)WJMenu{
    NSArray *dataArr = @[
                   @{@"title" : @"全部商家",
                     @"list" : @[@"Soldier", @"aaa0", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa1", @"aaa"]
                     },
                   @{@"title" : @"新店特惠",
                     @"list" : @[@"Soldier", @"aaa1", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa1", @"aaa"]
                     },
                   @{@"title" : @"连锁餐厅",
                     @"list" : @[@"Soldier", @"aaa2", @"aaa"]
                     },
                   @{@"title" : @"家常快餐",
                     @"list" : @[@"Soldier", @"aaa3", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa1", @"aaa"]
                     },
                   @{@"title" : @"地方菜",
                     @"list" : @[@"Soldier", @"aaa4", @"aaa"]
                     },
                   @{@"title" : @"特色小吃",
                     @"list" : @[@"Soldier", @"aaa5", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa1", @"aaa"]
                     },
                   @{@"title" : @"日韩料理",
                     @"list" : @[@"Soldier", @"aaa6", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa"]
                     }
                   ];

    //  如果是有导航栏请清除自动适应设置
    self.automaticallyAdjustsScrollViewInsets = NO;
    NSArray *threeMenuTitleArray =  @[@"分类",@"排序",@"筛选"];
    
    //  创建第一个菜单的first数据second数据
    NSMutableArray *firstArrOne = [NSMutableArray new];
    NSMutableArray *secondArrOne = [NSMutableArray new];
    for (NSDictionary *dic in dataArr) {
        [firstArrOne addObject:[dic objectForKey:@"title"]];
        [secondArrOne addObject:[dic objectForKey:@"list"]];
    }

    NSArray *firstMenu = [NSArray arrayWithObjects:firstArrOne,secondArrOne, nil];
    
    //  创建第二个菜单的first数据second数据
    NSArray *firstArrTwo = [NSArray arrayWithObjects:@"B一级菜单1",@"B一级菜单2", nil];
    NSArray *secondArrTwo = @[@[@"B二级菜单11",@"B二级菜单12"],@[@"B二级菜单21",@"B二级菜单22"]];
    NSArray *secondMenu = [NSArray arrayWithObjects:firstArrTwo,secondArrTwo, nil];
    
    //  创建第三个菜单的first数据second数据
    NSArray *firstArrThree = [NSArray arrayWithObjects:@"C一级菜单1",@"C一级菜单2", nil];
    NSArray *secondArrThree = @[@[@"C二级菜单11",@"C二级菜单12"],@[@"C二级菜单21",@"C二级菜单22",@"C二级菜单23",@"C二级菜单24"]];
    NSArray *threeMenu = [NSArray arrayWithObjects:firstArrThree,secondArrThree, nil];
    
    // 创建menu
    WJDropdownMenu *menu = [[WJDropdownMenu alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
    menu.delegate = self;         //  设置代理
    [self.view addSubview:menu];
    
    // 设置属性(可不设置)
    menu.caverAnimationTime = 0.2;//  增加了遮盖层动画时间设置 不设置默认是 0.15
    menu.menuTitleFont = 12;      //  设置menuTitle字体大小 默认不设置是  11
    menu.tableTitleFont = 11;     //  设置tableTitle字体大小 默认不设置是 10
    menu.CarverViewColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.5];//设置遮罩层颜色
    
    // 三组菜单调用方法
    [menu createThreeMenuTitleArray:threeMenuTitleArray FirstArr:firstMenu SecondArr:secondMenu threeArr:threeMenu];
}
#pragma mark tableView代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if(tableView==_myTableView)
    {
        return 1;
    }
    
    return 1;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView==_myTableView)
    {
        return self.modelArray.count;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Class currentClass = [MainTableViewCell class];
    MainTableViewCell *ceell = nil;
    MainDetailData *data = self.modelArray[indexPath.row];
    
    
    ceell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(currentClass)];
    
    
    ceell.data = data;
    
    return ceell;}

#pragma mark delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    DetailViewController *deta = [[DetailViewController alloc]init];
    deta.myTitle = @"买菜的";
    [self.navigationController pushViewController:deta animated:YES];
    
}

#pragma mark -- 代理方法返回点击时对应的index
- (void)menuCellDidSelected:(NSInteger)MenuTitleIndex firstIndex:(NSInteger)firstIndex andSecondIndex:(NSInteger)secondIndex{
    NSLog(@"菜单数:%ld          一级菜单数:%ld      二级子菜单数:%ld",MenuTitleIndex,firstIndex,secondIndex);
}

#pragma mark -- 代理方法返回点击时对应的内容
- (void)menuCellDidSelected:(NSString *)MenuTitle firstContent:(NSString *)firstContent andSecondContent:(NSString *)secondContent{
    NSLog(@"菜单title:%@       一级菜单:%@         二级子菜单:%@",MenuTitle,firstContent,secondContent);
    
}

-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
    
    
    self.tabBarController.tabBar.translucent = YES;
    self.tabBarController.tabBar.hidden = YES;
    
}

@end
